/*
 * Author Ahmed Abdelhalim - 2009
 * Email: englemo@hotmail.com
 * Please do not remove the above lines
 */

package remoteserver;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyVetoException;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;

/**
 *
 * @author Van Cong
 */
class ClientHandler extends Thread {

    private Socket cSocket = null;
    private JFrame frame;
    private JPanel cPanel = new JPanel();
    private  ClientScreenReciever clientScreenReciever;
    private ClientCommandsSender clientCommandsSender;            
    ObjectInputStream ois = null;
        
    public ClientHandler(Socket cSocket, JFrame frame1) {
        frame = frame1;
        this.cSocket = cSocket;
        start();
    }

    /*
     * Draw GUI per each connected client
     */
    public void drawGUI(){
        frame.setLayout(new BorderLayout());
        frame.getContentPane().add(cPanel,BorderLayout.CENTER);
        cPanel.setFocusable(true);
        frame.setVisible(true);
        Toolkit tk = Toolkit.getDefaultToolkit();  
        int xSize = ((int) tk.getScreenSize().getWidth());  
        int ySize = ((int) tk.getScreenSize().getHeight());  
        frame.setPreferredSize(new Dimension(xSize/2, ySize/2));        
        frame.setMinimumSize(new Dimension(xSize/3, ySize/3));
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {       
                try {
                    DataOutputStream dOStream = new DataOutputStream(cSocket.getOutputStream());
                    dOStream.writeUTF("close");
                    clientScreenReciever.closeConnect();                    
                    ois.close();
                    dOStream.close();
                    cSocket.close();
                } catch (IOException ex) {
                    Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public void run(){

        //used to represent client screen size
        Rectangle clientScreenDim = null;
        //Used to read screenshots and client screen dimension
        //start drawing GUI
        drawGUI();

        try{
            //Read client screen dimension
            ois = new ObjectInputStream(cSocket.getInputStream());
            clientScreenDim =(Rectangle) ois.readObject();
        }catch(IOException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }
        //Start recieveing screenshots
        clientScreenReciever = new ClientScreenReciever(ois,cPanel);
        //Start sending events to the client
        //clientCommandsSender = new ClientCommandsSender(cSocket,cPanel,clientScreenDim);
    }

}
